package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.*;
import com.xjh.t.domain.OrderInfo;
import com.xjh.t.domain.Organization;
import com.xjh.t.domain.Permission;
import com.xjh.t.domain.User;
import com.xjh.t.service.IOrganizationService;
import com.xjh.t.vo.OrganizationVo;
import com.xjh.t.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组织表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-06-01
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController {

    @Autowired
    private IOrganizationService organizationService;

    @RequestMapping("getAllOuputCenter")
    public DataGridView getAllOuputCenter(OrganizationVo organizationVo) {

        IPage<Organization> page = new Page<>(organizationVo.getPage(), organizationVo.getLimit());
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("id");
        this.organizationService.page(page, queryWrapper);

        List<TreeNode> nodes = new ArrayList<>();

        Integer index = 1;
        for (Organization organization : page.getRecords()) {
            TreeNode node = new TreeNode(organization.getId(), 0, organization.getSimplename(), false);
            nodes.add(node);
        }

        return new DataGridView(page.getTotal(), nodes);

    }

    @RequestMapping("loadAllOuputCenter")
    public DataGridView loadAllOuputCenter(OrganizationVo organizationVo) {

        IPage<Organization> page = new Page<>(organizationVo.getPage(), organizationVo.getLimit());
        QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("id");
        this.organizationService.page(page, queryWrapper);


        return new DataGridView(page.getTotal(), page.getRecords());

    }

    @RequestMapping("addManufacture")
    public ResultObj addManufacture(Organization organizationVo) {

        organizationVo.setOrgType(2);
        organizationVo.setNum(1);
        organizationVo.setPid(44);
        organizationVo.setTips("1");
        organizationVo.setPids("[0],[44]");
        this.organizationService.save(organizationVo);
        return ResultObj.ADD_SUCESS;
    }

    @RequestMapping("updateOrganization")
    public ResultObj updateOrganization(Organization organizationVo) {

        this.organizationService.updateById(organizationVo);
        return ResultObj.UPDATE_SUCESS;
    }

    @RequestMapping("deleteOrganization")
    public ResultObj deleteOrganization(Organization organizationVo) {

        this.organizationService.removeById(organizationVo.getId());
        return ResultObj.DELETE_SUCESS;
    }
}

